<?php

class Client_Table_Parters extends Apk_Db_Table
{
	protected $_name      = 'parters';
    protected $_primary   = 'id';
    protected $_ERROR = '';
    protected $_lang;

    public function __construct($lang = 'ru')
    {
    	$this->_lang = $lang;
    	parent::__construct();
    }

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetDataList($page=1,  $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'idt', 'id_cat', 'path', 'file_name', 'title' => 'title_'.$this->_lang, 'desc' => 'desc_'.$this->_lang))
    		  ->join('categories', 'categories.id = products.id_cat', array('url'))
    		  ->where($this->_name.'.is_active = 1')
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->order($this->_name.'.norder');

    	// **  Params  **
    	if(count($params) > 0)
    	foreach ($params as $k => $v)
    	switch($k)
    	{
    		case 'cats':
    			$sel->where('id_cat in (?)', $v);
    			break;

    		default:
    			$sel->where($this->_name.'.'.$k.' = ?', $v);
    	}

    	//echo $sel; die();

    	try{
    		return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    	}catch(Exception $e)
    	{
    		echo $e->__toString(); die();
    	}
    }

    public function GetLast($limit = 3)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name, array('id', 'idt', 'id_cat', 'path', 'file_name', 'art', 'title' => 'title_'.$this->_lang, 'desc' => 'desc_'.$this->_lang, 'body' => 'body_'.$this->_lang, 'info' => 'info_'.$this->_lang, 'is_show_gallery'))
    				->where('is_active')
    				->where('is_removed = 0')
    				->where('is_ghost = 0')
    				->order('public_date desc')
    				->limit($limit);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }


    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name, array('id', 'idt', 'id_cat', 'path', 'file_name', 'title' => 'title_'.$this->_lang, 'desc' => 'desc_'.$this->_lang, 'body' => 'body_'.$this->_lang, 'is_show_gallery'))
		    		->where($this->_name.'.is_active')
		    		->where($this->_name.'.is_removed = 0')
		    		->where($this->_name.'.is_ghost = 0');


    	if(intval($id) > 0) $sel->where($this->_name.'.id = ?', $id);
		else $sel->where($this->_name.'.idt = ?', $id);

		//echo $sel; die();

		try {
	    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
		}
		catch (Exception $e)
		{
			return array();
		}


    	return $data;
    }




    public function AddHit($id)
    {
    	$data = $this->GetDataRow($id);

    	$d['hit_count'] = $data['hit_count'] + 1;
    	$this->update($d, 'id = '.$id);

    	return true;
    }




    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            case 'unit':
				switch($value)
				{
				   case 'm2':
				   case 'м2':
				   		$result[$key] = 'm<sup>2</sup>';
				   		break;
				   case 'm3':
				   case 'м3':
				   		$result[$key] = 'm<sup>3</sup>';
				   		break;
				   case 'mm3':
				   		$result[$key] = 'mm<sup>3</sup>';
				   		break;
				   	case 'mm2':
				   		$result[$key] = 'mm<sup>2</sup>';
				   		break;
				   	default:
				   		$result[$key] = stripslashes($value);
				}
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

    public function topDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;

            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}
?>